package com.wy._05Random.randomFile;


import com.wy._05Random.RandomCharacter;
import com.wy._05Random.randomFile.Enum.FileType;
import com.wy._05Random.randomFile.Enum.FontEnum;
import com.wy._05Random.randomFile.randomPic.RandomPic;
import org.junit.Test;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Random;

public class test {
    public static void main(String[] args) throws IOException {
        long start = System.currentTimeMillis();
        ArrayList<BufferedImage> bufferedImages = new ArrayList<>();
        for (int i = 0; i < 100; i++) {
            BufferedImage randomImagesOfByte = RandomPic.getRandomImagesOfByte(new Random());
            bufferedImages.add(randomImagesOfByte);
        }
        long start2 = System.currentTimeMillis();
        System.out.println("cost ：" + (start2 - start));
        for (int i = 0; i < bufferedImages.size(); i++) {

            ImageIO.write(bufferedImages.get(i), FileType.ImageType.JPG.getTypeName(), new File("D:\\test\\pic" + "/" + i + ".jpg"));
        }
        long start3 = System.currentTimeMillis();
        System.out.println("cost ：" + (start3 - start2));

    }

    @Test
    public void test1() throws IOException {
        Random random = new Random();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 10; i++) {
            long l = System.currentTimeMillis();
            int nn = (i + 1) * 10;
            BufferedImage handle1 = handle(3000, 2000, getRandomColor(random), FileType.ImageType.PNG, nn);
            long l1 = System.currentTimeMillis();
            ImageIO.write(handle1, FileType.ImageType.JPG.getTypeName(), new File("D:\\test\\pic" + "/" + i + ".png"));
            long l2 = System.currentTimeMillis();
            System.out.println(nn + " cost1 :" + (l1 - l) + " cost1 :" + (l2 - l1));
        }
        long end = System.currentTimeMillis();
        System.out.println("cost ： " + (end - start));
    }

    public static BufferedImage handle(int width, int height, Color backgroundColor, FileType.ImageType imageType, int n) throws IOException {
        Random random = new Random();
        // 初始化图片
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        Graphics2D g2d = image.createGraphics();

        // 设置随机背景色 首先设置颜色，之后设置矩形
        g2d.setColor(backgroundColor);
        g2d.fillRect(0, 0, width, height);
        g2d.dispose();

        // 尝试写入到ByteArrayOutputStream以测量大小
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        // 写入图片到ByteArrayOutputStream
        ImageIO.write(image, imageType.getTypeName(), baos);
        byte[] imageBytes = baos.toByteArray();
        // 如果达到，则写入到文件并退出循环
        // 如果没有达到，则继续增加图片元素
        addRandomElementsToImage(image, random, n);
        //重置byteArray，为了下一次写入不受影响
        baos.reset();
        return image;

    }

    private static void addRandomElementsToImage(BufferedImage image, Random random, int n) {
        Graphics2D g2d = image.createGraphics();
        // 添加多个随机文本
        for (int i = 0; i < n; i++) {
            g2d.setFont(getRandomFont(random));
            g2d.setColor(getRandomColor(random));
            String randomText = RandomCharacter.getRandomStrEnCnNu(7, random, false);
            g2d.drawString(randomText, random.nextInt(image.getWidth()), random.nextInt(image.getHeight()));
        }

        // 绘制随机线条
        for (int i = 0; i < n; i++) {
            g2d.setColor(getRandomColor(random));
            g2d.drawLine(random.nextInt(image.getWidth()), random.nextInt(image.getHeight()), random.nextInt(image.getWidth()), random.nextInt(image.getHeight()));
        }

        //  // 绘制空心圆形
        //            Ellipse2D.Double circle = new Ellipse2D.Double(50, 50, width - 100, height - 100);
        //            g2d.setColor(Color.BLACK);
        //            g2d.draw(circle);
        //
        //            // 绘制空心方形
        //            Rectangle2D.Double square = new Rectangle2D.Double(100, 100, width - 200, height - 200);
        //            g2d.setColor(Color.BLACK);
        //            g2d.draw(square);
        //
        //            // 绘制空心椭圆形
        //            Ellipse2D.Double ellipse = new Ellipse2D.Double(200, 200, width - 400, height - 400);
        //            g2d.setColor(Color.BLACK);
        //            g2d.draw(ellipse);
        // 绘制随机形状
        for (int i = 0; i < n; i++) {
            g2d.setColor(getRandomColor(random));
            int shapeType = random.nextInt(3);
            int x = random.nextInt(image.getWidth());
            int y = random.nextInt(image.getHeight());
            int size = random.nextInt(15);
            switch (shapeType) {
                case 0: // 圆形
                    Ellipse2D.Double circle = new Ellipse2D.Double(random.nextInt(x)-20+random.nextInt(20), random.nextInt(y)+20, 200, 300);
                    g2d.draw(circle);
                    break;
                case 1: // 矩形
                    Rectangle2D.Double square = new Rectangle2D.Double(random.nextInt(x)-20+random.nextInt(20), random.nextInt(y)-20, 200, 300);
                    g2d.draw(square);
                    break;
                case 2: // 椭圆形
//                    g2d.fillOval(x, y, size, size);
                    break;
            }
        }
        g2d.dispose();
    }

//    10 cost1 :3483 cost1 :88
//20 cost1 :1034 cost1 :52
//30 cost1 :244 cost1 :71
//40 cost1 :487 cost1 :125
//50 cost1 :437 cost1 :134
//60 cost1 :233 cost1 :70
//70 cost1 :295 cost1 :49
//80 cost1 :241 cost1 :76
//90 cost1 :334 cost1 :51
//100 cost1 :417 cost1 :51

    /**
     * 得到一个随机的字体对象
     *
     * @param random 随机对象
     * @return 返回一个随机的字体对象
     */
    private static Font getRandomFont(Random random) {
        return new Font(LocalGraphicsEnvironment.getRandomFontName(), FontEnum.getRandomFontStyle(), LocalGraphicsEnvironment.getRandomFontSize());
    }

    /**
     * 得到一个随机 颜色对象
     *
     * @param random 随机对象
     * @return 返回一个随机颜色对象
     */
    private static Color getRandomColor(Random random) {
        return new Color(random.nextInt(256), random.nextInt(256), random.nextInt(256));
    }

}
